﻿using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;

namespace tools.req
{
    public partial class pst : Form
    {
        public pst()
        {
            InitializeComponent();
        }



        private void btn_post_Click(object sender, EventArgs e)
        {
            try
            {
                //参数p
                IDictionary<string, string> parameters = new Dictionary<string, string>();
                string txt = txt_canshu.Text.Trim() + "\r\n";
                if (txt != "" && txt.IndexOf("\r\n") != -1)
                {
                    string[] striparr = txt.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                    for (int i = 0; i < striparr.Length; i++)
                    {
                        if (striparr[i].IndexOf(":") != -1)
                        {
                            string[] m = striparr[i].Split(':');
                            parameters.Add(m[0], HttpUtility.UrlEncode(m[1]));
                        }

                    }
                }

                // http请求
                System.Net.HttpWebResponse res = htp.CreatePostHttpResponse(txt_dizhi.Text.Trim(), parameters, 3000, null, null);
                if (res == null)
                {
                    txt_str.Text = "出错了,可能是由于您的网络环境差、不稳定或安全软件禁止访问网络，您可在网络好时或关闭安全软件在重新访问网络。";
                }
                else
                {
                    //获取返回数据转为字符串
                    string mes = htp.GetResponseString(res);
                    txt_str.Text = mes;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void txt_txt_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string txt = txt_txt.Text;
                txt_canshu.Text = "";
                if (txt.IndexOf("?") != -1)
                {
                    string[] txtc = txt.Split('?');
                    txt_dizhi.Text = txtc[0];

                    if (txtc[1].IndexOf("&") != -1)
                    {
                        string[] alts = txtc[1].Split('&');
                        for (int i = 0; i < alts.Length; i++)
                        {
                            string[] clts = alts[i].Split('=');
                            if (clts.Length > 1)
                                txt_canshu.Text += clts[0] + ":" + clts[1] + "\r\n";
                        }
                    }
                    else
                    {
                        if (txtc[1].IndexOf("=") != -1)
                        {
                            string[] clts = txtc[1].Split('=');
                            if (clts.Length > 1)
                                txt_canshu.Text = clts[0] + ":" + clts[1] + "\r\n";
                        }
                    }

                }
                else
                {
                    txt_dizhi.Text = txt;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
